package dproxy.server.net.protocol;

import java.io.InputStream;
import java.util.Date;
import java.util.Map;

import dproxy.server.common.exceptions.ResponseReadException;

/**
 * Every protocol must implement this interface. 
 * A protocol must implement the logic to generate a key and to read 
 * requests and responses.
 */
public interface Protocol {

    /** Timeout of sockets used in this application. */
    int DEFAULT_SO_TIMEOUT = 20000;

    /**
     * Returns the protocol name (static information).
     * @return Protocol name
     */
    String getName();

    /**
     * Lifecicle callback method. Called after a Protocol is instantiated.
     */
    void init();
    
    /**
     * Sets a description (from XML configuration).
     * @param description to set
     */
    void setDescription(String description);
    
    /**
     * Gets protocol description (from XML configuration).
     * @return Protocol description
     */
    String getDescription();
    
    /**
     * Initialization parameters (from XML configuration).
     * @return Returns the initParams.
     */
    Map<String, String> getInitParams();

    /**
     * Initialization parameters (from XML configuration).
     * @param initParams The initParams to set.
     */
    void setInitParams(Map<String, String> initParams);
    
    /**
     * Implements key generation logic.
     * @param request Request contents
     * @return Key value
     */
    String generateKey(byte[] request);
    
    /**
     * Reads the request contents from stream to byte array.
     * @param inputStream input stream
     * @return byte array
     */
    byte[] readRequest(InputStream inputStream);
    
    /**
     * Reads the response contents from stream to byte array.
     * @param inputStream input stream
     * @return byte array
     * @throws ResponseReadException if the server responds an error condition
     */
    byte[] readResponse(InputStream inputStream) throws ResponseReadException;
    
    /**
     * Checks if a request has reached timeout.
     * @param requestDate Date of request reading
     * @return if timeout was reached
     */
    boolean timeout(Date requestDate);
}
